//
//  HSMessagesViewModel.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/10/11.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit

class HSMessagesViewModel {
    
    var message: HSMessagesModel {
        didSet {
            //点击回复或者点赞的时候重新计算
            likeNameAttribute = likeAttributeWith(str: message.likelist ?? "")
            setupBgViewH()
            computeCellH()
        }
    }
    
    var replayAttributeArray:[NSAttributedString] = []
    //点赞人名的富文本
    var likeNameAttribute:NSAttributedString = NSAttributedString()
    //因为当前类型没有继承任何父类，所以不存在重写父类方法
    
    //计算整个cell的高度
    var cellHeight: CGFloat = 0
    
    var replayViewH: CGFloat = 0
    
    init(model: HSMessagesModel) {
        message = model
        //这里处理属性计算和高度计算
        likeNameAttribute = likeAttributeWith(str: message.likelist ?? "")
        setupBgViewH()
        computeCellH()
    }
    
    fileprivate func computeCellH() {
        let maxSize = CGSize(width: replayViewW, height: CGFloat(MAXFLOAT))
        let contentH = ((message.not_content ?? "") as NSString).boundingRect(with: maxSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 13)], context: nil).size.height
        let titleH = ((message.not_title ?? "") as NSString).boundingRect(with: maxSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15)], context: nil).size.height
        
        cellHeight = contentH + titleH + replayViewH + 148
        
    }
    //计算回复视图
    fileprivate func setupBgViewH() {
        //高度 = 点赞Label的top + 点赞内容高度 + 线高 + （间距 + 回复l高）* 数量 + 间距
        // 创建一个总高度的变量
        let maxSize = CGSize(width: replayViewW, height: CGFloat(MAXFLOAT))
        
        var viewH: CGFloat = 0
        let replayNum = message.replaylist?.count ?? 0
        if message.likenum > 0 || replayNum > 0 {
            viewH += 12 + 5
        }
        
        if message.likenum > 0 && replayNum > 0 {
            viewH += 6
        }
        
        if message.likenum > 0 {
            let likeStrSize = likeNameAttribute.boundingRect(with: maxSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil).size.height
            
            viewH += likeStrSize
        }
        if let replayList = message.replaylist {
            //创建一个可变数组
            var attrArrayM = [NSAttributedString]()
            
            for model in replayList {
                //获得模型
                let replayM = model as! HSReplaylistModel
                //先将字符串和名称转换为富文本
                let attrName = NSMutableAttributedString(string: (replayM.int_name ?? "") + ":", attributes: [NSForegroundColorAttributeName:UIColor().colorWithHex(0x00a0e9),NSFontAttributeName:UIFont.systemFont(ofSize: 13)])
                let attrConent = NSMutableAttributedString(string: replayM.int_content ?? "", attributes: [NSForegroundColorAttributeName:UIColor().colorWithHex(0x4c4c4c),NSFontAttributeName:UIFont.systemFont(ofSize: 13)])
                //拼接
                attrName.append(attrConent)
                attrArrayM.append(attrName)
                
                let replayStrH = attrName.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, context: nil).size.height
                viewH += (5 + replayStrH)
            }
            replayAttributeArray = attrArrayM
            //加上最后
            
        }
        replayViewH = viewH
    }
    
    //计算属性和高度
    fileprivate func likeAttributeWith(str: String) -> NSAttributedString {
        //
        if message.likenum == 0 {
            return NSAttributedString()
        }
        
        let attach = NSTextAttachment()
        attach.image = UIImage(named: "notifi_support_press")
        attach.bounds = CGRect(x: 0, y: -1, width: 13, height: 13)
        //将点赞字符串转换为富文本
        let likeStrAttribute = NSAttributedString(string: (string: " :" + str) as! String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 13)])
        let imageStr = NSAttributedString(attachment: attach)
        
        let attributeM = NSMutableAttributedString(attributedString: imageStr)
        attributeM.append(likeStrAttribute)
        
        return attributeM
    }
    
}
